﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;
using System.Data;
using ThuVien;
namespace TranTekBE.InGia
{
    public partial class PriceService : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string datamatrix = Request.QueryString["datamatrix"];
            string value = Request.QueryString["value"];

            if (datamatrix != null)
            {
                string type_MaCode = "Sp.C001205";
                string MaCode = datamatrix;
                if (datamatrix.Contains(";"))
                {
                    type_MaCode = "Sp.C001203";
                    string[] items_POS = Regex.Split(datamatrix, ";");
                    MaCode = items_POS[0];
                }
//                string sql = @"select top 10 Sp.C001203 as 'MaSP' ,Sp.C001205 as 'MaVach'
//                                 , Sp.C001215 as 'DVT' 
//                                 , Gia.C000704 as 'Gia' from T0012 SP
//                                Left Join T0007 Gia
//                                on SP.C001200 = Gia.C000701
//                                where " + type_MaCode + @" = '" + MaCode + @"' and Gia.C002000 = " + value + @" order by Gia.C000704 asc";


                string sql = @"select Sp.C001203 as 'MaSP' , Sp.C001205 as 'MaVach' ,
                                Gia.C000704 as 'Gia', DVT.C004001 as DonViTinh 
                            from T0012 SP 
                            Left Join T0007 Gia
                                on SP.C001200 = Gia.C000701
                            left join T0040 as DVT
                                on Gia.C000702 = DVT.C004000
                            where " + type_MaCode + @" = '" + MaCode + @"' and Gia.C002000 = " + value + @" order by Gia.C000704 asc";

                DataSet ds = FunctionsUtils.Database.ExecuteQuery(sql);
                int count_rows = ds.Tables[0].Rows.Count;
                if (count_rows > 0)
                {
                    string gia_SP = null;

                    for (int i = 0; i < count_rows; i++)
                    {
                        if (gia_SP == null)
                            gia_SP = FunctionsUtils.ConvertMoneyVND(ds.Tables[0].Rows[i]["Gia"].ToString()) + " " + ds.Tables[0].Rows[i]["DonViTinh"].ToString() + ";";
                        else
                        {
                            gia_SP = gia_SP + "|" + Convert.ToDouble(ds.Tables[0].Rows[i]["Gia"].ToString())/1000;
                        }
                    }
                    price.Text = gia_SP.Replace(";|",";");

                }
                else
                    price.Text = "-1";
            }
            else
            {
                price.Text = "-2";
            }
        }
       
    }
}